package com.hblack.common.httpclient;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 发送httpclient请求工具
 * @author Hblack
 *
 */
@Service
public class ApiService implements BeanFactoryAware{

	@Autowired(required=false)
	private RequestConfig requestConfig;
	
	/**
	 * 无参的get请求
	 * @param url
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public String doGet(String url) throws ClientProtocolException, IOException{
		HttpGet httpGet = new HttpGet(url);
		
		httpGet.setConfig(requestConfig);
		
		CloseableHttpResponse response = null;
		
		try {
			response = getHttpClient().execute(httpGet);
			if(response.getStatusLine().getStatusCode() == 200){
				return EntityUtils.toString(response.getEntity(), "UTF-8");
			}
			return null;
		}finally{
			if(response != null) {
				response.close();
			}
		}
	}
	
	/**
	 * 有参doGet请求
	 * @param url
	 * @param params
	 * @return
	 * @throws URISyntaxException
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public String doGet(String url,Map<String, String> params) throws URISyntaxException, ClientProtocolException, IOException{
		
		URIBuilder uriBuilder = new URIBuilder(url);
		if(params != null){
			for (String key : params.keySet()) {
				uriBuilder.setParameter(key, params.get(key));
			}
		}
		URI uri = uriBuilder.build();
		return this.doGet(uri.toString());
		
	}
	
	/**
	 * 有参doPost请求
	 * @param url
	 * @param params
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public HttpResult doPost(String url,Map<String, String> params) throws ClientProtocolException, IOException {
		
		HttpPost httpPost = new HttpPost(url);
		
		if(params != null) {
			ArrayList<NameValuePair> parameters = new ArrayList<>();
			
			for(String key : params.keySet()){
				parameters.add(new BasicNameValuePair(key, params.get(key)));
			}
			
			UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
			httpPost.setEntity(formEntity);
		}
		
		httpPost.setConfig(requestConfig);
		
		CloseableHttpResponse response = null;
		
		try {
			response = this.getHttpClient().execute(httpPost);
			return new HttpResult(response.getStatusLine().getStatusCode(), EntityUtils.toString(response.getEntity(), "utf-8"));
			
		} finally {
			if(response != null) {
				response.close();
			}
		}
		
	}
	
	/**
	 * 请求体是json的post请求
	 * @param url
	 * @param json
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public HttpResult doPostJson(String url,String json) throws ClientProtocolException, IOException{
		
		HttpPost httpPost = new HttpPost(url);
		
		if(StringUtils.isNotBlank(json)){
			StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);
			httpPost.setEntity(stringEntity);
		}
		
		httpPost.setConfig(requestConfig);
		
		CloseableHttpResponse response = null;
		
		try {
			response = this.getHttpClient().execute(httpPost);
			return new HttpResult(response.getStatusLine().getStatusCode(), EntityUtils.toString(response.getEntity(), "utf-8"));
			
		} finally {
			if(response != null) {
				response.close();
			}
		}
		
	}
	
	/**
	 * 无参post请求
	 * @param url
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public HttpResult doPost(String url) throws ClientProtocolException, IOException {
		
		return doPost(url, null);
	}
	
	
	
	
	
	private BeanFactory beanFactory;
	
	@Override
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		this.beanFactory = beanFactory;
	}
	
	public CloseableHttpClient getHttpClient(){
		return beanFactory.getBean(CloseableHttpClient.class);
	}
}
